﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb
{
    public partial class login1 : System.Web.UI.Page
    {
        Maticsoft.BLL.UserAdmin userbll = new Maticsoft.BLL.UserAdmin();
        Maticsoft.BLL.UserRoleRelation urr = new Maticsoft.BLL.UserRoleRelation();
        BLL.News news = new BLL.News();
        Maticsoft.BLL.Role rolebll = new Maticsoft.BLL.Role();
        BLL.DealersManage dealersManagebll = new BLL.DealersManage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["UserLogin"] != null)
                {
                    if (new Maticsoft.BLL.UserAdmin().GetModel(Convert.ToInt32(Request.Cookies["UserLogin"].Values[0])).loginname == "admin")
                    {//管理员可以查看所有
                        Repeater1.DataSource = news.GetList(5, "", "createtime desc ");
                    }
                    else
                    {
                        Repeater1.DataSource = news.GetList(5, " showobject=0 or showobject=" + Convert.ToInt32(Request.Cookies["UserLogin"].Values[0]) + " ", "createtime desc ");
                    }

                }//Cookies为空 查询针对所有经销商的新闻公告
                else
                {
                    Repeater1.DataSource = news.GetList(5, " showobject=0  ", "createtime desc ");
                }

                messageBox.Visible = false;
                //Repeater1.DataSource = news.GetList(5, "", "createtime desc");
                Repeater1.DataBind();
            }
        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(username.Text))
            {
                messageBox.Visible = true;
                txtmessage.Text = "请输入用户名!";
                return;
            }
            else if (string.IsNullOrWhiteSpace(password.Text))
            {
                messageBox.Visible = true;
                txtmessage.Text = "请输入密码!";
                return;
            }
            else if (string.IsNullOrWhiteSpace(verifycode.Value))
            {
                messageBox.Visible = true;
                txtmessage.Text = "请输入验证码!";
                return;
            }
            List<Maticsoft.Model.UserAdmin> userlist = userbll.GetModelList(string.Format("loginname='{0}'", username.Text));
            if (userlist.Count < 1)
            {
                messageBox.Visible = true;
                txtmessage.Text = "该用户不存在!";
                return;
            }
            else if (userlist[0].userstate == "已锁定")
            {
                messageBox.Visible = true;
                txtmessage.Text = "已锁定账户，联系管理员解锁";
                return;
            }
            else
            {
                if (userlist[0].password.Equals(password.Text))
                {

                    if (Session["CheckCode"] != null)
                    {
                        string checkcode = Session["CheckCode"].ToString();
                        if (this.verifycode.Value == checkcode)
                        {
                            BO.log.WriteTextLog("222", "53s56", new DateTime());
                            Maticsoft.Model.Role role = rolebll.GetModel(urr.GetModel(string.Format("userid='{0}'", userlist[0].id)).roleid.Value);
                            HttpCookie cookie = new HttpCookie("UserLogin");
                            Model.DealersManage dm = dealersManagebll.GetModel(Convert.ToInt32(userlist[0].dealersid));
                            cookie.Values.Add("ID", userlist[0].id.ToString());
                            cookie.Values.Add("username", Server.UrlEncode(userlist[0].username.ToString()));
                            cookie.Values.Add("loginname", Server.UrlEncode(userlist[0].loginname.ToString()));
                            cookie.Values.Add("rolecodemenber", Server.UrlEncode(role.rolecode));
                            cookie.Values.Add("rolename", Server.UrlEncode(role.rolename));
                            cookie.Values.Add("dmid", dm.Id.ToString());
                            cookie.Values.Add("dmmobile", dm.mobile.ToString());
                            cookie.Values.Add("dmname", Server.UrlEncode(dm.username));
                            cookie.Expires = DateTime.Now.AddDays(1);
                            Response.AppendCookie(cookie);
                            Server.Transfer("index.aspx");
                        }
                        else
                        {
                            messageBox.Visible = true;
                            txtmessage.Text = "验证码输入错误!";
                            return;
                        }
                    }
                }
                else
                {

                    Maticsoft.Model.UserAdmin ne = userbll.GetModel(userlist[0].id);
                    Int32 errlogin = Convert.ToInt32(ne.logincount);//原错误次数
                        errlogin += 1;
                        ne.logincount = errlogin;
                        userbll.Update(ne);
                        if (errlogin == 5)
                        {
                            ne.userstate = "已锁定";
                            userbll.Update(ne);
                            messageBox.Visible = true;
                            txtmessage.Text = "您已输入密码错误" + errlogin + "次，已锁定账户，联系管理员解锁!";
                        }
                        else {
                            messageBox.Visible = true;
                            txtmessage.Text = "您已输入密码错误" + errlogin + "次，密码错误5次将锁定账户!";
                        }

                }
            }
        }
    }
}